#pragma once
#include <memory>
#include "Common.hpp"
#include "Log.hpp"
#include "InetAddr.hpp"

//这里我们来自行封装一下套接字

namespace mySocket{
    using namespace myLog;
    
    const int _default_sockfd = -1;
    const int _default_backlog = 16;


    class Socket{
    public:
        virtual void SocketOrDie() = 0;
        virtual void BindOrDie(uint16_t port) = 0;
        virtual void ListenOrDie(int backlog) = 0;
        virtual std::shared_ptr<Socket> AcceptOrDie(InetAddr* client) = 0;
        virtual int Recv(std::string* inbuffer) = 0;
        virtual int Send(std::string message) = 0;
        virtual void Connect(InetAddr& addr) = 0;
        virtual int Close() = 0;

    public:
        void CreateServerTcpSocket(uint16_t port, int backlog = _default_backlog){
            SocketOrDie();
            BindOrDie(port);
            ListenOrDie(backlog);
        }

        void CreateClientTcpSocket(){
            SocketOrDie();
        }
    };


    const int default_sockfd = -1;
    const int default_backlog = 16;

    class TcpSocket : public Socket{
    public:
        TcpSocket()
            :_sockfd(_default_sockfd)
        {}

        TcpSocket(int sockfd)
            :_sockfd(sockfd)
        {}

        virtual void SocketOrDie() override {
            _sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
            if(_sockfd < 0){
                LOG(LogLevel::FATAL) << "socket error";
                exit(SOCKET_ERROR);
            }
            LOG(LogLevel::INFO) << "socket success";
        }

        virtual void BindOrDie(uint16_t port) override {
            InetAddr server(port);
            int bind_ret = ::bind(_sockfd, server.GetSockAddr_Ptr(), server.GetAddrSize());
            if(bind_ret < 0){
                LOG(LogLevel::FATAL) << "bind error";
                exit(BIND_ERROR);
            }
            LOG(LogLevel::INFO) << "bind success";
        }

        virtual void ListenOrDie(int backlog) override {
            int listen_ret = ::listen(_sockfd, backlog);
            if(listen_ret < 0){
                LOG(LogLevel::FATAL) << "listen error";
                exit(LISTEN_ERROR);
            }
            LOG(LogLevel::INFO) << "listen success";
        }

        virtual std::shared_ptr<Socket> AcceptOrDie(InetAddr* client) override {
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);
            int accept_ret = ::accept(_sockfd, CONVERT(&peer), &len);
            if(accept_ret < 0){
                LOG(LogLevel::WARNING) << "accpet warning...";
                return nullptr;
            }
            client->SetAddr(peer);
            LOG(LogLevel::INFO) << "accpet success : " << (*client).GetFormatStr();
            return std::make_shared<TcpSocket>(accept_ret);
        }

        virtual int Recv(std::string* inbuffer){
            char buffer[1024] = {0};
            int n = recv(_sockfd, buffer, sizeof(buffer) - 1, 0);
            if(n > 0) *inbuffer += buffer;
            return n;
        }

        virtual int Send(std::string message) override {
            return send(_sockfd, message.c_str(), message.size(), 0);
        }

        virtual void Connect(InetAddr& addr) override {
            int n = connect(_sockfd, addr.GetSockAddr_Ptr(), addr.GetAddrSize());
            if(n < 0){
                LOG(LogLevel::FATAL) << "Connect error";
                exit(CONNECT_ERROR);
            }
            LOG(LogLevel::INFO) << "Connect success";
        }

        virtual int Close() override {
            return ::close(_sockfd);
        }

        ~TcpSocket(){}
    private:
        int _sockfd; //这个sockfd可能是listen_sockfd，也可能用来IO的文件描述符
    };
}